import { Component, OnInit } from '@angular/core'
import { Router } from '@angular/router'
import { MineService } from './services/mine.service'
import { CoolLocalStorage } from 'src/app/common/coolStorage/cool-local-storage'
import { CoolSessionStorage } from 'src/app/common/coolStorage/cool-session-storage'

@Component({
  selector: 'app-index',
  templateUrl: './index.component.html',
  styleUrls: ['./index.component.scss']
})
export class IndexComponent implements OnInit {
  // 个人中心列表
  list = []
  // 个人资料
  userinfo: any
  // 是否登录
  login = false

  constructor(
    private router: Router,
    private service: MineService,
    private storage: CoolLocalStorage,
    private session: CoolSessionStorage
  ) {}

  ngOnInit() {
    let userinfo = this.storage.getObject('userinfo')
    if (userinfo) {
      this.userinfo = userinfo
      this.login = true
    } else {
      this.getCurrentUser()
    }
    this.getEntryList()
  }

  // 路由跳转
  nTo(url) {
    this.router.navigate([url])
  }
  // 获取个人中心列表
  getEntryList() {
    this.list = this.service.getEntryList()
  }
  // 获取个人资料
  getCurrentUser() {
    this.service.getCurrentUser(
      res => {
        if (res.Success && res.Result) {
          let Photo = 'assets/images/House/profile_user_info_photo.png'
          if (res.Result.Photo) {
            Photo = JSON.parse(res.Result.Photo)[0].Items[0]
          }
          res.Result.Photo = Photo
          this.userinfo = res.Result
          this.login = true
          this.storage.setObject('userinfo', res.Result)
        }
      },
      () => {}
    )
  }
  // 路由分发
  handleRouter(url) {
    let whltelist = ['houseLoan', 'taxation']
    // 白名单内的直接跳转，其他根据登录状态进行拦截
    if (whltelist.indexOf(url) > -1 || this.login) {
      this.nTo(`/workspace/mine/${url}`)
    } else {
      this.nTo('/register')
    }
  }
}
